package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	public class GMDefault implements IGameMode
	{
		private var nextItem:GameObject;
		private var displayPoint:Sprite = new Sprite();
		private var m_parentDisplay:DisplayObjectContainer;
		private var m_inCooldown:Boolean = false;
		private var m_cooldownTimer:Timer = new Timer(500);
		public function GMDefault(parentDisplay:DisplayObjectContainer)
		{
			m_cooldownTimer.addEventListener(TimerEvent.TIMER, onCooldownTimer);
			m_parentDisplay = parentDisplay;
			setupNextItem();
		}
		
		private function onCooldownTimer(te:TimerEvent):void{
			m_inCooldown = false;
			m_cooldownTimer.stop();
			setupNextItem();
		}
		
		public function overrideNextItem():void{
			setupNextItem();
		}
		
		private function setupNextItem():void{
			
			var currentMousePosition:Point = new Point(Game.stage.mouseX, Game.stage.mouseY);
			var lastPosition:Point = new Point(Math.floor(currentMousePosition.x/Game.PPS), Math.floor(currentMousePosition.y/Game.PPS));
			
			nextItem = Game.instance.getNextItem();
			m_parentDisplay.addChild(nextItem.display);
			nextItem.display.alpha = .8;
			nextItem.forceLocation(lastPosition.clone());
		}

		public function onMouseMove(me:MouseEvent):void{
			if(m_inCooldown){
				return;
			}
			
			var currentMousePosition:Point = new Point(me.stageX, me.stageY);
			var gamePoint:Point = new Point(Math.floor(currentMousePosition.x/Game.PPS), Math.floor(currentMousePosition.y/Game.PPS));
			//var clampedPosition:Point = Util.clamp(currentMousePosition, Game.PPS);
			

			nextItem.forceLocation(gamePoint);
			nextItem.updateDisplayPosition();
		}
		
		public function onMouseUp(me:MouseEvent):void{
			if(m_inCooldown){
				return;
			}	
			nextItem.display.alpha = 1;
			Game.instance.placeNextItem();
			m_inCooldown = true;
			m_cooldownTimer.start();
		}
		
		public function onMouseDown(me:MouseEvent):void{

		}
		
		public function cleanup():void{}
	}
}